package vamp_serviceweb

import org.springframework.dao.DataIntegrityViolationException

class VehicleController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    //Différente valeur qu'un status peut avoir


    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [vehicleInstanceList: Vehicle.list(params), vehicleInstanceTotal: Vehicle.count()]
    }

    def create() {
        [vehicleInstance: new Vehicle(params)]
    }

    def save() {
        def constructeurInstance = Constructor.get(params.constructor.id)
        def vehiculeInstance = new Vehicle(description: params.description, model: params.model,year: params.year,imguri: params.imguri)
        def statusInstance = new Status(status: Vehicle.STATUS_AVAILABLE, inprocess: 0, available: 1)
        def vehiculestatusInstance = new Vehiclestatus(comment: "initial", vehicle: vehiculeInstance, status: statusInstance)
        constructeurInstance.addToVehicules(vehiculeInstance)
        statusInstance.save(flush: true)
        vehiculestatusInstance.save(flush: true)
        //Initialisation du status du véhicule
        if (!constructeurInstance.save(flush: true)) {
            render(view: "create", model: [vehicleInstance: vehiculeInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), vehiculeInstance.id])
        redirect(action: "show", id: vehiculeInstance.id)
    }

    def show() {
        def vehicleInstance = Vehicle.get(params.id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), params.id])
            redirect(action: "list")
            return
        }

        [vehicleInstance: vehicleInstance]
    }

    def edit() {
        def vehicleInstance = Vehicle.get(params.id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), params.id])
            redirect(action: "list")
            return
        }

        [vehicleInstance: vehicleInstance]
    }

    def update() {
        def vehicleInstance = Vehicle.get(params.id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (vehicleInstance.version > version) {
                vehicleInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'vehicle.label', default: 'Vehicle')] as Object[],
                        "Another user has updated this Vehicle while you were editing")
                render(view: "edit", model: [vehicleInstance: vehicleInstance])
                return
            }
        }

        vehicleInstance.properties = params

        if (!vehicleInstance.save(flush: true)) {
            render(view: "edit", model: [vehicleInstance: vehicleInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), vehicleInstance.id])
        redirect(action: "show", id: vehicleInstance.id)
    }

    def delete() {
        def vehicleInstance = Vehicle.get(params.id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), params.id])
            redirect(action: "list")
            return
        }

        try {
            vehicleInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), params.id])
            redirect(action: "show", id: params.id)
        }
    }

    //Service de gestion du status du véhicule
}
